home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / EC / OperationNames / serverOpNames.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-16  |  5.3 KB  |  220 lines

  1. #include "assert.h"
  2. #include "emTypes.h"
  3. #include "system.h"
  4. #include <sys/file.h>
  5. #include "opNames.h"
  6. #include <stdio.h>
  7.  
  8. #ifndef EMDIR
  9. #define EMDIR "/scratch/eric/emerald/"
  10. #endif
  11.  
  12. static char FILENAME[] = "/EC/OperationNames/Names";
  13.  
  14. typedef unsigned int HashValue;
  15. #define MULTIPLIER 17
  16.  
  17. static void CheckOutHashTable();
  18. static char *FileName;
  19. static HashValue Hash(s)
  20. register char *s;
  21. {
  22.   register HashValue value = 0;
  23.   while (*s) value = value * MULTIPLIER + *s++;
  24.   return(value);
  25. }
  26.  
  27. static struct {
  28.   int hashTableSize;
  29.   int hashTableSizeInBytes;
  30.   int charTableMaxSize;
  31.   int charTableCurrentSize;
  32.   OID nextOIDToAllocate;
  33.   OID maxOIDToAllocate;
  34. } header;
  35.  
  36. typedef struct OpNameEntry {
  37.   int offset;            /* into charTable of the string */
  38.   OID id;            /* what we want */
  39. } HashTableEntry;
  40.  
  41. static HashTableEntry *hashTable;
  42. static char *charTable;
  43.  
  44. void ON_initialize()
  45. {
  46.   int fd;
  47.   
  48.   FileName = (char *) malloc((unsigned)(strlen(EMDIR)+strlen(FILENAME)+1));
  49.   (void) strcpy(FileName, EMDIR);
  50.   (void) strcat(FileName, FILENAME);
  51.   fd = open(FileName, O_RDONLY, 0);
  52.   assert(fd != -1);
  53.   if (read(fd, (char *)&header, sizeof(header)) != sizeof(header)) assert(0);
  54.   charTable = (char *) malloc((unsigned)header.charTableMaxSize);
  55.   hashTable = (HashTableEntry *) malloc((unsigned)header.hashTableSizeInBytes);
  56.   if (read(fd, (char *)hashTable, header.hashTableSizeInBytes) != 
  57.     header.hashTableSizeInBytes) assert (0);
  58.   if (read(fd, charTable, header.charTableMaxSize) !=
  59.     header.charTableMaxSize) assert(0);
  60.   if (close (fd) != 0) assert(FALSE);
  61. #ifdef lint
  62.   CheckOutHashTable();
  63. #endif
  64. }
  65.  
  66. void ON_finalize()
  67. {
  68.   int fd;
  69.   fd = open(FileName, O_WRONLY, 0);
  70.   assert (fd != -1);
  71.   if (write(fd, (char *)&header, sizeof(header)) != sizeof(header)) assert(0);
  72.   if (write(fd, (char *)hashTable, header.hashTableSizeInBytes) != 
  73.     header.hashTableSizeInBytes) assert(0);
  74.   if (write(fd, charTable, header.charTableMaxSize) != 
  75.     header.charTableMaxSize) assert(0);
  76.   if (close(fd) != 0) assert(FALSE);
  77. }
  78.  
  79. OID AllocateOID()
  80. {
  81.   assert (header.nextOIDToAllocate <= header.maxOIDToAllocate);
  82.   return(header.nextOIDToAllocate++);
  83. }
  84.  
  85. OID AllocateOIDS()
  86. {
  87.   OID result;
  88.   assert (header.nextOIDToAllocate < header.maxOIDToAllocate - OIDBLOCKSIZE);
  89.   result = header.nextOIDToAllocate;
  90.   header.nextOIDToAllocate += OIDBLOCKSIZE;
  91.   return(result);
  92. }
  93.  
  94. static int SaveString(s)
  95. register char *s;
  96. {
  97.   int i = strlen(s), result;
  98.   register char *t;
  99.   
  100.   if (header.charTableCurrentSize + i + 1 > header.charTableMaxSize) {
  101.     t = (char *)malloc((unsigned)(2 * header.charTableMaxSize));
  102.     bcopy(charTable, t, (unsigned)header.charTableCurrentSize);
  103.     free(charTable);
  104.     charTable = t;
  105.     header.charTableMaxSize *= 2;
  106.   }
  107.   result = header.charTableCurrentSize;
  108.   t = charTable + header.charTableCurrentSize;
  109.   header.charTableCurrentSize += (i + 1);
  110.   while (*s) *t++ = *s++;
  111.   *t = '\0';
  112.   return(result);
  113. }
  114.  
  115. static void ExpandHashTable()
  116. {
  117.   register HashTableEntry *nh, *oe, *ne;
  118.   register int oldHashTableSize = header.hashTableSize, i;
  119.   register HashValue hash;
  120.   int index;
  121.  
  122.   header.hashTableSize *= 2;
  123.   header.hashTableSizeInBytes *= 2;
  124.   nh = (HashTableEntry *) malloc((unsigned)header.hashTableSizeInBytes);
  125.   bzero((char *)nh, header.hashTableSizeInBytes);
  126.   for (i = 0; i < oldHashTableSize; i++) {
  127.     oe = &hashTable[i];
  128.     hash = Hash(charTable + oe->offset);
  129.     index = hash % header.hashTableSize;
  130.     assert (index >= 0 && index < header.hashTableSize);
  131.     while (1) {
  132.       ne = &nh[index];
  133.       if (ne->id == 0) {
  134.     ne->id = oe->id;
  135.     ne->offset = oe->offset;
  136.     break;
  137.       } else {
  138.     index++;
  139.     if (index >= header.hashTableSize) index = 0;
  140.       }
  141.     }
  142.   }
  143.   free((char *)hashTable);
  144.   hashTable = nh;
  145. }
  146.  
  147. OID ON_Translate(s)
  148. char *s;
  149. {
  150.   register HashValue hash = Hash(s);
  151.   int index = hash % header.hashTableSize;
  152.   int firstIndex = index;
  153.   register HashTableEntry *e;
  154.  
  155.   while (1) {
  156.     e = &hashTable[index];
  157.     if (e->id == 0) {        /* we didn't find it */
  158.       e->id = AllocateOID();
  159.       e->offset = SaveString(s);
  160.       return (e->id);
  161.     } else if (!strcmp(s, charTable + e->offset)) {
  162.       return (e->id);
  163.     } else {
  164.       index ++;
  165.       if (index >= header.hashTableSize) index = 0;
  166.       if (index == firstIndex) {
  167.     ExpandHashTable();
  168.     return(ON_Translate(s));
  169.       }
  170.     }
  171.   }
  172. }
  173.  
  174. static void CheckOutHashTable()
  175. {
  176.   int i;
  177.   HashValue hash;
  178.   register HashTableEntry *realElement, *e;
  179.   int index, firstIndex;
  180.  
  181.   for (i = 0; i < header.hashTableSize; i++) {
  182.     realElement = &hashTable[i];
  183.     if (realElement->id != 0) {
  184.       hash = Hash(charTable + realElement->offset);
  185.       index = hash % header.hashTableSize;
  186.       firstIndex = index;
  187.       while (1) {
  188.     e = &hashTable[index];
  189.     if (e->id == 0) {        /* we didn't find it */
  190.       break;
  191.     } else if (!strcmp(charTable + realElement->offset, 
  192.       charTable + e->offset)) {
  193.       break;
  194.     } else {
  195.       index++;
  196.       if (index >= header.hashTableSize) index = 0;
  197.       if (index == firstIndex) {
  198.         index = -1;
  199.       }
  200.     }
  201.       }
  202.       fprintf(stderr,
  203.     "Element \"%s\", hash %x, rightIndex %d, realIndex %d\n",
  204.     charTable+realElement->offset, hash, firstIndex, index);
  205.     }
  206.   }
  207. }
  208.  
  209. char *ON_Name(id)
  210. OID id;
  211. {
  212.   register int i;
  213.   register HashTableEntry *te;
  214.   for (i = 0; i < header.hashTableSize; i++) {
  215.     te = &hashTable[i];
  216.     if (te->id == id) return (charTable + te->offset);
  217.   }
  218.   return(NULL);
  219. }
  220.